Component({
    properties: {
        show: {
            type: Boolean,
            value: !1
        },
        mode: {
            type: String,
            value: "job"
        },
        params: {
            type: Object,
            value: {}
        }
    },
    data: {
        hideCanvas: !0
    },
    ready: function() {
        var t = this.data.params.sourcePath;
        console.log(t), this.imginit(t);
    },
    methods: {
        imginit: function(t) {
            var a = this;
            wx.getImageInfo({
                src: t,
                success: function(t) {
                    var s = wx.getSystemInfoSync(), i = a.getScale(a.data.params.ctxW, a.data.params.ctxH, t.width, t.height), e = Math.max(1.8, i), o = Math.min(e, a.getScale(s.windowWidth, s.windowHeight, t.width, t.height)), h = a.data.params.ctxW - t.width * o, n = a.data.params.ctxH - t.height * o, d = parseInt(.5 * h), c = parseInt(.5 * n);
                    a.setData({
                        maxX: 0,
                        maxY: 0,
                        minX: h,
                        minY: n,
                        posX: d,
                        posY: c,
                        zoomStart: o,
                        zoomMin: i,
                        zoomMax: e,
                        oldimg: t.path,
                        oldht: t.height,
                        oldwd: t.width
                    });
                }
            });
        },
        seletImage: function() {
            var t = this, a = wx.createCanvasContext("imgcanvas", t);
            a.clearRect(0, 0, t.data.params.ctxW, t.data.params.ctxH), a.draw(), wx.chooseImage({
                count: 1,
                sizeType: [ "compressed" ],
                success: function(a) {
                    var s = a.tempFilePaths[0];
                    t.imginit(s);
                }
            });
        },
        summitAvatar: function() {
            var t = this;
            this.drawImage().then(function(a) {
                console.log(a), t.triggerEvent("uploadpic", {
                    msg: "success",
                    res: a
                });
            }).catch(function(a) {
                t.triggerEvent("uploadpic", {
                    msg: "fail",
                    res: a
                });
            });
        },
        drawImage: function() {
            var t = this, a = this;
            return new Promise(function(s, i) {
                a.setData({
                    hideCanvas: !1
                });
                var e = wx.createCanvasContext("imgcanvas", t);
                e.clearRect(0, 0, a.data.params.ctxW, a.data.params.ctxH), e.save(), e.drawImage(a.data.oldimg, a.data.posX, a.data.posY, a.data.oldwd * a.data.zoomStart, a.data.oldht * a.data.zoomStart), 
                e.draw(!1, function() {
                    wx.canvasToTempFilePath({
                        canvasId: "imgcanvas",
                        success: function(t) {
                            a.setData({
                                hideCanvas: !0
                            });
                            var e = t.tempFilePath;
                            e.length <= 0 ? i("请选择一张图片") : s(e);
                        },
                        fail: function(t) {
                            i(t);
                        }
                    }, a);
                });
            });
        },
        getScale: function(t, a, s, i) {
            var e = t / s, o = a / i;
            return e > o ? e : o;
        },
        getDis: function(t) {
            var a = t[0].pageX - t[1].pageX, s = t[0].pageY - t[1].pageY;
            return Math.sqrt(a * a + s * s);
        },
        etouchstart: function(t) {
            this.data.oldimg && (this.move = 1 == t.touches.length, this.zoom = t.touches.length >= 2, 
            this.sttouches = t.touches, this.touches = t.touches, 2 == this.touches.length && (this.mangles = [ this.angles(this.touches) ]));
        },
        etouchmove: function(t) {
            if (this.data.oldimg) {
                if (this.move) {
                    var a = t.touches[0].pageX - this.touches[0].pageX, s = t.touches[0].pageY - this.touches[0].pageY;
                    this.touches = t.touches, this.setData({
                        time: 0,
                        posX: this.data.posX + a,
                        posY: this.data.posY + s
                    });
                }
                if (this.zoom) {
                    if (this.touches.length >= 2 && t.touches.length >= 2) {
                        var i = this.getDis(this.touches), e = .004 * (this.getDis(t.touches) - i), o = this.data.zoomStart + e;
                        o > this.data.zoomMax && (o = this.data.zoomMax), o < this.data.zoomMin && (o = this.data.zoomMin);
                        var h = this.data.params.ctxW - this.data.oldwd * this.data.zoomStart, n = this.data.params.ctxH - this.data.oldht * this.data.zoomStart, d = this.data.posX + this.data.params.ctxW * (this.data.zoomStart - o), c = this.data.posY + this.data.params.ctxH * (this.data.zoomStart - o);
                        this.setData({
                            zoomStart: o,
                            minX: h,
                            minY: n,
                            posX: parseInt(d),
                            posY: parseInt(c)
                        });
                    }
                    this.touches = t.touches, 2 == this.touches.length && this.mangles.push(this.angles(this.touches));
                }
            }
        },
        etouchend: function() {
            this.data.oldimg && (this.data.posX > this.data.maxX ? this.data.posX = this.data.maxX : this.data.posX < this.data.minX && (this.data.posX = this.data.minX), 
            this.data.posY > this.data.maxY ? this.data.posY = this.data.maxY : this.data.posY < this.data.minY && (this.data.posY = this.data.minY), 
            this.setData({
                posX: this.data.posX,
                posY: this.data.posY,
                time: 600
            }));
        },
        angles: function(t) {
            var a = Math.abs(t[0].pageX - t[1].pageX), s = Math.abs(t[0].pageY - t[1].pageY), i = 180 * Math.atan(s / a) / Math.PI;
            return t[1].pageY > t[0].pageY && t[1].pageX < t[0].pageX || t[0].pageY > t[1].pageY && t[0].pageX < t[1].pageX ? 180 - i : i;
        }
    }
});